﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Net.NetworkInformation;

namespace Code4WP7SDK
{
    public class NetStateHelper
    {

        /// <summary>
        /// 判断网络是否仍处于连接状态
        /// </summary>
        /// <returns></returns>
        public static bool NetWorkIsConnecting()
        {
            bool isConnecting = true;
            isConnecting = NetworkInterface.GetIsNetworkAvailable();
            if (isConnecting)
            {
                NetworkInterfaceType currentNetworkType = NetworkInterface.NetworkInterfaceType;
                if (currentNetworkType == NetworkInterfaceType.None)
                {
                    isConnecting = false;
                }
            }
            return isConnecting;
        }

        /// <summary>
        /// 获取粗粒度的网络状态
        /// </summary>
        /// <returns></returns>
        public static NetStateType GetCoarsnessState()
        {
            NetworkInterfaceType netWorkType = NetworkInterface.NetworkInterfaceType;
            switch (netWorkType)
            {
                case NetworkInterfaceType.MobileBroadbandCdma:
                    return NetStateType.CDMA;
                case NetworkInterfaceType.MobileBroadbandGsm:
                    return NetStateType.CSM;
                case NetworkInterfaceType.Wireless80211:
                    return NetStateType.Wifi;
                case NetworkInterfaceType.Ethernet:
                    return NetStateType.Ethernet;
                case NetworkInterfaceType.None:
                    return NetStateType.None;
                default :
                    return NetStateType.Other;
            }
        }
        /// <summary>
        /// 获取细粒度的网路状态
        /// </summary>
        /// <returns></returns>
        public static NetStateType GetNetThinState()
        {
            NetStateType thinStateType = NetStateType.Other;
            DeviceNetworkInformation.ResolveHostNameAsync(new DnsEndPoint("http://www.baidu.com", 80),
                new NameResolutionCallback(handler =>
                {
                    NetworkInterfaceInfo info = handler.NetworkInterface;
                    if (info != null)
                    {
                        switch (info.InterfaceType)
                        {
                            case NetworkInterfaceType.Ethernet:
                                thinStateType = NetStateType.Ethernet;
                                break;
                            case NetworkInterfaceType.MobileBroadbandCdma:
                            case NetworkInterfaceType.MobileBroadbandGsm:
                                switch (info.InterfaceSubtype)
                                {
                                    case NetworkInterfaceSubType.Cellular_3G:
                                    case NetworkInterfaceSubType.Cellular_EVDV:
                                    case NetworkInterfaceSubType.Cellular_EVDO:
                                    case NetworkInterfaceSubType.Cellular_HSPA:
                                        thinStateType = NetStateType._3G;
                                        break;
                                    case NetworkInterfaceSubType.Cellular_GPRS:
                                    case NetworkInterfaceSubType.Cellular_EDGE:
                                    case NetworkInterfaceSubType.Cellular_1XRTT:
                                        thinStateType = NetStateType._2G;
                                        break;
                                    default:
                                        thinStateType = NetStateType.None;
                                        break;
                                }
                                break;
                            case NetworkInterfaceType.Wireless80211:
                                thinStateType = NetStateType.Wifi;
                                break;
                            default:
                                thinStateType = NetStateType.None;
                                break;

                        }
                    }
                    else
                    {
                        thinStateType = NetStateType.None;
                    }
                }), null);
            return thinStateType;
        }

    }

    public enum NetStateType
    {
        CDMA,
        CSM,
        Wifi,
        Ethernet,
        None,
        _3G,
        _2G,
        Other,
    }
}
